<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

class RequestDataUtil
{
    
    function toInt(&$arr, $keys) {
        foreach($keys as $k => $v) {
            if(array_key_exists($v, $arr)) {
                $arr[$v] = (int) $arr[$v];
            }
        }
        
        return $arr;
    }
    
    
    function _getRules($type = 'html') {
        
		// order does matter !
		$a['html']['search']  = array('&quot;', '&#039;', '&gt;', '&lt;', '&amp;');
        $a['html']['replace'] = array('"',      "'",      '>',    '<',    '&');
		
		// order does matter !
        $a['xml']['search']  = array('&amp;', '&quot;', '&apos;', '&apos;',    '&gt;', '&lt;');
        $a['xml']['replace'] = array('&',     '"',      "'",      '&#039;',    '>',    '<');		
        
		return $a[$type];
    }
    	
    
    function _stripVar(&$value, $magic_quotes_gpc, $rules, $server_check = false, $wordrap_num = 0) {
        
        // to db
        if(empty($server_check) || $server_check === 'stripslashes' || $server_check === 'addslashes') {
            if($wordrap_num===0) {
                $value = trim(str_replace($rules['search'], $rules['replace'], $value));
            } else {
                $value = wordwrap(trim(str_replace($rules['search'], $rules['replace'], $value)), $wordrap_num, ' ', 1);
            }
        	if(!$magic_quotes_gpc || $server_check === 'addslashes') {
				$value = addslashes($value);
			}
			
			// to other sources as email for example
			if($server_check === 'stripslashes') {
				$value = stripslashes($value);
			}
		
        // to display
        } else {
            $value = str_replace($rules['search'], $rules['replace'], $value); // for compability
			$value = htmlspecialchars($value);// ENT_QUOTES - both single and double quotes are translated
        	
			// only if we show it after form submission
			if($magic_quotes_gpc && $server_check !== 'display') {
				$value = stripslashes($value);
	        }
		}
        
        return $value;
    }	
	
	
    function &stripVars(&$arr, $skip_keys = array(), $server_check = false, $wordrap_num = 0) {
        
        $rules = RequestDataUtil::_getRules();
		$magic_quotes_gpc = get_magic_quotes_gpc();
        
        if(!is_array($arr)) {
            return $arr = RequestDataUtil::_stripVar($arr, $magic_quotes_gpc, $rules, $server_check, $wordrap_num);
        }
        
        foreach($arr as $k => $v) {
            if(is_array($v)) {
                $arr[$k] = RequestDataUtil::stripVars($v, $skip_keys, $server_check, $wordrap_num);
            } else {
                if(in_array($k, $skip_keys)) {$arr[$k] = $arr[$k]; continue; }                
                $arr[$k] = RequestDataUtil::_stripVar($arr[$k], $magic_quotes_gpc, $rules, $server_check, $wordrap_num);
            }
        }
        
		//echo "<pre>"; print_r($_POST); echo "</pre>";
        //echo "<pre>"; print_r($arr); echo "</pre>";
        return $arr;
    }
	
	
    function _stripVarXml(&$value, $rules_html, $rules_xml, $wordrap_num = 0) {

        if($wordrap_num===0) {
        $value = trim(str_replace($rules_html['search'], $rules_html['replace'], $value));
        } else {
        $value = wordwrap(trim(str_replace($rules_html['search'], $rules_html['replace'], $value)), $wordrap_num, ' ', 1);
        }
        $value = str_replace($rules_xml['replace'], $rules_xml['search'], $value);
        
        return $value;
    }	
	
	
    function &stripVarsXml(&$arr, $wordrap_num = 0) {
        
        $rules_html = RequestDataUtil::_getRules('html');
		$rules_xml = RequestDataUtil::_getRules('xml');
        
        if(!is_array($arr)) {
            return $arr = RequestDataUtil::_stripVarXml($arr, $rules_html, $rules_xml, $wordrap_num);
        }
        
        foreach($arr as $k => $v) {
            if(is_array($v)) {
                $arr[$k] = RequestDataUtil::stripVarsXml($v, $wordrap_num);
            } else {
				// if(in_array($k, $skip_keys)) {$arr[$k] = $arr[$k]; continue; }         
                $arr[$k] = RequestDataUtil::_stripVarXml($arr[$k], $rules_html, $rules_xml, $wordrap_num);
            }
        }
        
        //echo "<pre>"; print_r($arr); echo "</pre>";
        return $arr;
    }	
    
    
    function _stripVarHtml(&$value, $magic_quotes_gpc, $rules, $server_check = false) {
        
        // to db
        if(empty($server_check)) {
			if(!$magic_quotes_gpc) {
				$value = addslashes($value);
			}
        
        // to display // only if we show it after form submission
        } elseif($magic_quotes_gpc && $server_check !== 'display') {
			$value = stripslashes($value);
        }
        
		//echo "<pre>"; print_r($value); echo "</pre>";
        return $value;
    }
    
    
    function &stripVarsHtml(&$arr, $skip_keys = array(), $server_check = false) {
        
		$rules = RequestDataUtil::_getRules();
		$magic_quotes_gpc = get_magic_quotes_gpc();
        
		if(!is_array($arr)) {
            return $arr = RequestDataUtil::_stripVarHtml($arr, $magic_quotes_gpc, $rules, $server_check);
        }
        
        foreach($arr as $k => $v){
            if(is_array($v)) {
                $arr[$k] = RequestDataUtil::stripVarsHtml($v, $skip_keys, $server_check);
            } else {
                if(in_array($k, $skip_keys)) {$arr[$k] = $arr[$k]; continue; }
                $arr[$k] = RequestDataUtil::_stripVarHtml($arr[$k], $magic_quotes_gpc, $rules, $server_check);
            }
        }
        
        return $arr;
    }
	
	
/*
	// probably required when insert in db after HTML editor
	function &langEntityDecode(&$str, $lang = null) {
	
		$rules = RequestDataUtil::_getRules('lang_entity_decode');
	
		// ENT_COMPAT Will convert double-quotes and leave single-quotes alone. 
		// ENT_QUOTES Will convert both double and single quotes. 
		// ENT_NOQUOTES Will leave both double and single quotes unconverted. 

		
		// order does matter !
		$a['lang_entity_decode']['search'] = array('&gt;', '&lt;', '&amp;');
        $a['lang_entity_decode']['replace'] = array('>',    '<',    '&');		
		
		$str = &html_entity_decode($str, ENT_NOQUOTES, $lang);
		$str = &str_replace($rules['search'], $rules['replace'], $str);
	
		return $str;
	} 	
	
	
	function cleanTabsLines($string) {
		$string = preg_replace('/[\r\n]+/s',"\n", preg_replace('/[\r\n][ \t]+/s',"\n",$string)); 
		return $string;
	}
*/
}

/*
'&' (ampersand) becomes '&amp;' 
'"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set. 
''' (single quote) becomes '&#039;' only when ENT_QUOTES is set. 
'<' (less than) becomes '&lt;' 
'>' (greater than) becomes '&gt;'
*/


/*
$data = array('html'=>'<br>adasd " dflksdfk',
              'text'=>'jjhasjk " laksdlk <>');
              
              
echo "<pre>"; print_r(RequestDataUtil::stripVarsHtml($data['html'])); echo "</pre>";
echo "<pre>"; print_r(RequestDataUtil::stripVars($data, array(), true)); echo "</pre>";
*/
?>
